﻿using PiUi.View;
using System.Windows.Forms;
using PiControls.Controls.myTabControl;
using TestDatatable;
using PiFramework;
using System.Collections.Generic;
using System;
using System.IO;
namespace PiUi.Froms
{
    public partial class DebugView : BaseView
    {
        CustomTabControl tabControl = null;

        List<PJsCodeEdit> themesList = new List<PJsCodeEdit>();
        public DebugView()
        {
            InitializeComponent();
            tabControl = new CustomTabControl();
            PNotificationCenter.DefaultCenter().AddObserver("ChangeComboBox", this.OnNotification);
        }

        public override void LoadTheme() {
            tabControl.setPrimaryColor(ThemeColor.PrimaryColor);
            tabControl.setSecondaryColor(ThemeColor.SecondaryColor);
             for (int i = 0; i < themesList.Count; i++)
             {
                themesList[i].setPrimaryColor(ThemeColor.PrimaryColor);
                themesList[i].setSecondaryColor(ThemeColor.SecondaryColor);
                themesList[i].LoadTheme();
             }
            tabControl.Invalidate();
        }

        private void DebugView_Load(object sender, System.EventArgs e)
        {
            tabControl.Parent = this;
            tabControl.Dock = DockStyle.Fill;
            tabControl.setPrimaryColor(ThemeColor.PrimaryColor);
            tabControl.setSecondaryColor(ThemeColor.SecondaryColor);
            tabControl.Show();
        }

        public void OnNotification(string name, object sender, Dictionary<string, object> userInfo)
        {
            if (this.IsHandleCreated)
            {
                Invoke(new Action(() =>
                {
                    if (name == "ChangeComboBox")
                    {
                        for (int i = 0; i < themesList.Count; i++)
                        {
                            themesList[i].Dispose();
                        }
                        tabControl.TabPages.Clear();
                        string comboBoxName = userInfo["comboBoxName"] as string;
                        Dictionary<string, string> themes  = PJintThreadControl.Instance.GetThreadMapjsCode();
                        if (themes.Count == 0) {
                        
                            Console.WriteLine("没有找到任何pi.js文件");
                            return; 
                        }
                        foreach (var item in themes)
                        {
                            string folderName = item.Key;
                            string jsCode = item.Value;
                            PJsCodeEdit codeEdit1 = new PJsCodeEdit(); 
                            themesList.Add(codeEdit1);
                            TabPage tabPage1 = new TabPage(folderName);
                            tabPage1.Font = new System.Drawing.Font("微软雅黑", 10, System.Drawing.FontStyle.Bold);
                            tabPage1.Controls.Add(codeEdit1);
                            codeEdit1.Dock = DockStyle.Fill;
                            codeEdit1.ShowJsCode(jsCode);
                            tabControl.TabPages.Add(tabPage1);
                            codeEdit1.setPrimaryColor(ThemeColor.PrimaryColor);
                            codeEdit1.setSecondaryColor(ThemeColor.SecondaryColor);
                            codeEdit1.LoadTheme();
                        }
                    }
                }));
            }
        }
    }
}
